package com.yanzhu.service.impl;

import com.yanzhu.service.RedPacketsService;
import com.yanzhu.service.mapper.GrabRedPacketsMapper;
import com.yanzhu.service.mapper.RedPacketsMapper;
import com.yanzhu.service.mapper.WalletMapper;
import com.yanzhu.service.model.RedPackets;
import com.yanzhu.service.model.mix.GrabRedPacketsDetail;
import com.yanzhu.service.model.mix.RedPacketsDetail;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: 方炎柱
 * @Description:
 * @Date: Created in 15:54 2018/1/29
 * @Modified:
 */
@AllArgsConstructor
@Service
public class RedPacketsServiceImpl implements RedPacketsService {
    private RedPacketsMapper redPacketsMapper;

    private GrabRedPacketsMapper grabRedPacketsMapper;

    private WalletMapper walletMapper;

    @Override
    public int insert(RedPackets redPackets) {
        return redPacketsMapper.insert(redPackets);
    }

    @Override
    @Transactional
    public void distributeRedPackets(RedPackets redPackets, float money) {
        redPacketsMapper.insert(redPackets);
        walletMapper.update(redPackets.getUid(), money);
    }

    @Override
    public RedPacketsDetail redPacketsDetail(long vid) {
        RedPacketsDetail redPacketsDetail = redPacketsMapper.queryRedPacketsDetail(vid);
        List<GrabRedPacketsDetail> grabRedPacketsDetails = grabRedPacketsMapper.queryGrabRedPacketsDetail(vid);
        if(redPacketsDetail != null)
            redPacketsDetail.setGrabRedPacketsDetails(grabRedPacketsDetails);

        return redPacketsDetail;
    }
}
